﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using Vefskil5.Models;
using System.IO;
using System.Net;
using System.Configuration;
using System.Text;

namespace Vefskil5.Controllers
{
    public class ValuesController : ApiController
    {
        List<AudioBlaster3000> songs = new List<AudioBlaster3000>();
        
        public AudioBlaster3000 Get(int id)
        {
 	        AudioBlaster3000 song = songs.Find(x => x.Id == id);
            return song;
        }


        // GET /api/values
        public IEnumerable<AudioBlaster3000> Get()
        {
            IEnumerable<string> songPathList = Directory.GetFiles(ConfigurationManager.AppSettings["upload"]);


            List<TagLib.File> mp3files = new List<TagLib.File>();
            foreach (var item in songPathList)
            {
                mp3files.Add(TagLib.File.Create(item));
            }


            List<AudioBlaster3000> songs = new List<AudioBlaster3000>();
            foreach (var i in mp3files)
            {
                songs.Add(new AudioBlaster3000 { Artist = i.Tag.Performers.ToString(),
                                                 Title = i.Tag.Title.ToString(), 
                                                 Genre = i.Tag.Genres.ToString(),
                                                 FileName = i.ToString()
                });
            }


            return songs;

            
        }

        // POST /api/values
        public void Post()
        {
            var files = HttpContext.Current.Request.Files;
            var destFolder = ConfigurationManager.AppSettings["upload"];

            if (!Directory.Exists(destFolder))
            {
                Directory.CreateDirectory(destFolder);
            }

            foreach (var item in files.AllKeys)
            {
                var postedFile = files[item];
                string serverPath = HttpContext.Current.Server.MapPath("~");
                string songsPath = serverPath + "\\Songs\\" + postedFile.FileName;
                //    postedFile.SaveAs(destFolder + postedFile.FileName);                
                files[item].SaveAs(songsPath);
                TagLib.File file = TagLib.File.Create(songsPath);
                TagLib.Tag tager = file.GetTag(TagLib.TagTypes.Id3v2);
            

            }
                
        }


        // PUT /api/values/5
        public void Put(int id)
        {
            var files = HttpContext.Current.Request.Files;
            var destFolder = @"C:\Songs";

            if(!Directory.Exists(destFolder))
            {
                Directory.CreateDirectory(destFolder);
            }
            
            foreach( var item in files.AllKeys )
            {
                var postedFile = files[item];
                postedFile.SaveAs(destFolder);
                TagLib.File file = TagLib.File.Create(postedFile.FileName);
                TagLib.Tag tag = file.GetTag(TagLib.TagTypes.Id3v2);
            }
            
        }
}
        
    /*
        // DELETE /api/values/5
        public void Delete(int id)
        {
        }
}
        }*/
}